package com.erp.service.entity;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.alibaba.fastjson.serializer.SerializeWriter;
/**
 * dotnet日期序列化类
 * @author ccl
 *
 */
public class DotnetDateSerializer implements ObjectSerializer {

	/**
	 * 序列化器实例
	 */
    public final static DotnetDateSerializer instance = new DotnetDateSerializer();
    /**
     * 实现反序列化方法
     * @param serializer 序列化器
     * @param object 序列化对象
     * @param clazz 转换类型
     * @param fieldName 字段名
     * @param fieldType 字段类型
     */
    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType) throws IOException {
        SerializeWriter out = serializer.getWriter();

        if (object == null) {
            out.writeNull();
            return;
        }
        Date date = (Date) object;
        out.write("\"\\/Date(");
        
        Calendar calendar= Calendar.getInstance();
        calendar.setTime(date);
        out.write(Long.toString(calendar.getTimeInMillis()+TimeZone.getDefault().getRawOffset()));
        out.write(")\\/\"");
    }

    @Override
    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {

    }
}